package org.spring.springboot.controller;

import org.json.JSONArray;
import org.spring.springboot.domain.GraphFile;
import org.spring.springboot.service.GraphService;
import org.spring.springboot.service.IShareFileSvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import net.sf.json.JSONObject;
import org.json.JSONObject;


@RestController
@RequestMapping("/graph") // 这里定义了总的路径 "/api"
public class GraphController {

    @Autowired
    private GraphService graphService;

    @Autowired
    private IShareFileSvc shareFileSvc;


    @PostMapping("/getfiledata")
        public Map<String, Object> getfiledata(@RequestBody Map<String, Object> jsonMap) {
        String uuid = (String) jsonMap.get("uuid");
        GraphFile file = graphService.selectOne(uuid);
        String fileDataStr = file.getData();
        file.setData("");
        JSONObject fileInfoObj = file.toJson();
        JSONObject retJson = new JSONObject();
        retJson.put("info",fileInfoObj);//返回文件概要信息
        retJson.put("data",fileDataStr);//返回图形文件详细内容
        return retJson.toMap();
    }


    @PostMapping("/getAll")
    public List<Object> getAll(@RequestBody Map<String, Object> jsonMap) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String userid = jsonObject.getString("userid");

        List<GraphFile> list= graphService.selectAll(userid);
        System.out.println(list.size());

        List<GraphFile> sharelist= shareFileSvc.selectAll(userid);

        JSONArray arr = new JSONArray();
        for (GraphFile p:list) {
            JSONObject obj = p.toJson();
            arr.put(obj);
        }

        for (GraphFile p:sharelist) {
            JSONObject obj = p.toJson();
            arr.put(obj);
        }

        return arr.toList();
    }

    @PostMapping("/save")
    public Map<String, Object> save(@RequestBody Map<String, Object> jsonMap) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String uuid = jsonObject.getString("uuid");

        GraphFile fileObj = new GraphFile();
        fileObj.setUuid(jsonObject.getString("uuid"));

        String dataObj = jsonObject.getString("data");
        fileObj.setData(dataObj);

        fileObj.setSname(jsonObject.getString("name"));
        fileObj.setParent(jsonObject.getString("parent"));

        fileObj.setMaintype(jsonObject.getString("maintype"));
        fileObj.setFiletype(jsonObject.getString("filetype"));
        fileObj.setFolder(jsonObject.getString("folder"));
        fileObj.setIconURL(jsonObject.getString("iconURL"));

        fileObj.setUserid(jsonObject.getString("userid"));
        fileObj.setExtInfo(jsonObject.getString("extInfo"));

         graphService.save(fileObj);

        JSONObject retJson = new JSONObject();
        retJson.put("uuid",uuid);
        retJson.put("ret",true);
        return retJson.toMap();
    }

    @PostMapping("/updateFileData")
    public Map<String, Object> updateFileData(@RequestBody Map<String, Object> jsonMap) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String uuid = jsonObject.getString("uuid");

        String fileData = jsonObject.getString("data");
        String iconURL = jsonObject.getString("iconURL");
        String extInfo = jsonObject.getString("extInfo");

        graphService.updateData(uuid,fileData,iconURL,extInfo);

        JSONObject retJson = new JSONObject();
        retJson.put("uuid",uuid);
        retJson.put("ret",true);
        return retJson.toMap();
    }

    @PostMapping("/updateProfile")
    public Map<String, Object> update(@RequestBody Map<String, Object> jsonMap) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String uuid = jsonObject.getString("uuid");
        String parent = jsonObject.getString("parent");
        String name = jsonObject.getString("name");
        String iconURL = jsonObject.getString("iconURL");

        graphService.updateProfile(uuid,name,parent,iconURL);

        JSONObject retJson = new JSONObject();
        retJson.put("uuid",uuid);
        return retJson.toMap();
    }

    @PostMapping("/delete")
    public Map<String, Object> delete(@RequestBody Map<String, Object> jsonMap) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String uuid = jsonObject.getString("uuid");
        String name = jsonObject.getString("name");

        graphService.delete(uuid);

        JSONObject retJson = new JSONObject();
        retJson.put("uuid",uuid);
        return retJson.toMap();
    }



    @PostMapping("/getProject")
    public List<Object> getProject(@RequestBody Map<String, Object> jsonMap) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String pid = jsonObject.getString("pid");
        String userid = jsonObject.getString("userid");

        List<GraphFile> list= graphService.selectChild(pid,userid);
        System.out.println(list.size());

        JSONArray arr = new JSONArray();
        for (GraphFile p:list) {
            JSONObject obj = p.toJson();
            arr.put(obj);
        }
        return arr.toList();
    }


}
